<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/16
 * author : weblinuxgame
 * filename: UserApiTokenService.php
 */

namespace App\Services;

use App\Models\Token;

/**
 * Class UserApiTokenService
 * @package App\Services
 */
class UserApiTokenService extends BaseService
{
    protected $options = [];

    protected function __construct($options = [])
    {
        $this->options = $options;
    }

    /**
     * 存储
     * @param $data
     * @param array $options
     * @return Token
     */
    public function store($data, $options = []): Token
    {
        return new Token($data, empty($options) ? $this->options : $options);
    }

    /**
     * 验证
     * @param string $token
     * @return bool
     */
    public function verify(string $token): bool
    {
        return Token::verify($token);
    }

    /**
     * 加载
     * @param string $token
     * @return Token|null
     */
    public function load(string $token)
    {
        return Token::load($token);
    }

    /**
     * 续约
     * @param string $token
     * @param int $time
     * @return bool
     */
    public function keep(string $token, int $time = Token::ExpireTime): bool
    {
        return Token::storage()->expire($token, (int)$this->options['expire'] ?? $time);
    }

    /**
     * 删除
     * @param string $token
     * @return bool
     */
    public function delete(string $token): bool
    {
        return Token::storage()->del($token);
    }
}
